package cnm.validator.xpath.address;
/*
 * Created on Sep 17, 2008 
 *  
 * COPYRIGHT (C) 2008, ZyXEL Corporation (WuXi) Co., Ltd                  
 * Protected as an unpublished work. All Rights Reserved.
 * ZYXEL PROPRIETARY/CONFIDENTIAL.                                               
 * 
 * The computer program listings, specifications, and 
 * documentation herein are the property of ZYXEL 
 * Corporation and shall not be reproduced, copied, 
 * disclosed, or used in whole or in part for any reason 
 * without the prior express written permission of     
 * ZYXEL Corporation.                                    
 */

import java.util.Arrays;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.NodeList;

import cnm.validator.xpath.XPathValidator;

/**
 * 
 * @author Harry
 * @version
 *
 */
public class AddressObjectXPath extends XPathValidator {

    private static final Logger logger = Logger.getLogger(AddressObjectXPath.class);

    @Override
    public void xpathCheck() {
        logger.debug("Start XPath validate...");
        final String[] INTERFACE_TYPE ={"INTERFACE SUBNET", "RANGE", "INTERFACE IP", "HOST", "SUBNET"};
        final List<String> iface_type_list = Arrays.asList(INTERFACE_TYPE);
        
        if (iface_type_list == null || iface_type_list.size()==0) {
            logger.debug("no xpath validation in:" + this.getClass().getName());
            return;
        }
        
        XPathFactory xf = XPathFactory.newInstance();
          XPath path = xf.newXPath();
          XPathExpression exprOfType = null;
          Object resultOfType = null;
          try {
              exprOfType = path.compile("//DATASET/ATTRIBUTE[@name='_Type']/text()");
              
              resultOfType = exprOfType.evaluate(createDoc(), XPathConstants.NODESET);
              NodeList nodes = (NodeList) resultOfType;
              for (int i = 0; i < nodes.getLength(); i++) {
                  String value = nodes.item(i).getNodeValue();
                  if (!iface_type_list.contains(value)) {
                      logger.debug("[Validate fail]: " + value + " is not supported");
                  }
              }

          } catch (XPathExpressionException e) {
              e.printStackTrace();
          }
          logger.debug("End XPath validate...");
    }

}
